import { Alert } from "../../../../../com/mui/controls/Alert";
import { GTextFormat } from "../../../../common/GTextFormat";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { BaseItem } from "../../common/BaseItem";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { ShopConfig } from "../../../resource/ShopConfig";
import { ShopRule } from "../../../rules/ShopRule";
import { ItemData } from "../../../resource/info/ItemData";
import { Cache } from "../../../cache/Cache";
import { EquipPropButton } from "../../equipment/deploy/EquipPropButton";
import { PropItem } from "./PropItem";
import { ItemConfig } from "../../../resource/ItemConfig";
import { MsgManager } from "../../../manager/MsgManager";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { AcupointData } from "./AcupointData";
import { SystemSetter } from "../../system/SystemSetter";
import { QuickBuyWindow } from "../../common/QuickBuyWindow";
import { SAttributeUpdate } from "../../../../../Message/Public/SAttributeUpdate";
import { AcupointConfig } from "../../../resource/AcupointConfig";
import { RoleCache } from "../../../cache/RoleCache";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { GTextInput } from "../../../../../com/mui/controls/GTextInput";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { ShopItemInfo } from "../../../resource/info/ShopItemInfo";
import { TAcupointConfig } from "../../../../../Message/Db/Tables/TAcupointConfig";
import { _shopItemInfo } from "../../../../../this/_shopItemInfo";
import { _step } from "../../../../../this/_step";
type int = number;
//class AcupointRootUpgrade
    
    export  class AcupointRootUpgrade /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.init();
            return;
        }

        /* internal  */onPerfectPropItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._perfectPropBtn.visible = true;
            this._perfectPropItem.visible = false;
            this._successRateTxt.text = this._tAcupointConfig.rate + "%";
            return;
        }

        /* internal  */onTipClickHandler(arg1: int): void
        {
            Alert.resetToDefault();
            return;
        }

        /* internal  */init(): void
        {
            var loc10=null;
            this._yellowTf = new GTextFormat("", 12, 16776960);
            this._yellowSTf = new GTextFormat("", 12, 15859633, null, null, null, null, null, null, null, null, null, 5);
            this._yellowBTf = new GTextFormat("", 12, 16750848);
            this._greenSTf = new GTextFormat("", 12, 65280);
            this._greenBTf = new GTextFormat("", 20, 65280);
            var loc1=ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB);
            loc1.width = 279;
            loc1.height = 395;
            addChild(loc1);
            var loc2=ObjCreate.createBg(0, 65, 274, 135, this, ImagesConst.Friend_over);
            var loc3=Language.getString(30139);
            var loc4=this.createTextfield(65, 155, 100, 60, loc3, this._yellowSTf);
            var loc5=ObjCreate.createBg(10, 240, 250, 2, this, ImagesConst.SplitLine);
            var loc6=new GImageBitmap("equipment_successRate.png", 70, 70);
            this.addChild(loc6);
            var loc7=ObjCreate.createBitmap(ImagesConst.PackItemBg, 35, 97, this);
            var loc8=ObjCreate.createBitmap(ImagesConst.MultiIcon, 80, 110, this);
            this._item = new BaseItem();
            this._item.move(38, 100);
            this._item.setSize(32, 32);
            this._item.isDragAble = false;
            this._item.isDropAble = false;
            addChild(this._item);
            this._numInput = ObjCreate.createGTextInput(100, 107, 40, 20, this);
            this._numInput.restrict = "0-9";
            this._numInput.text = "1";
            this._buyBtn = ObjCreate.createGButton(Language.getString(30124), 150, 105, 40, 20, this);
            this._buyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBuyBtnClickHandler);
            this._upgradeBtn = ObjCreate.createGButton(Language.getString(30104), 160, 210, 70, 20, this);
            this._upgradeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onUpgradeBtnClickHandler);
            this._usePropCb = ObjCreate.createCheckBox(Language.getString(30126), 40, 210, 200, 20, this, "CheckBoxStyle");
            this._usePropCb.setStyle("textFormat", new GTextFormat("", 12, 65280));
            this._usePropCb.addEventListener(flash.events.Event.CHANGE, this.onUsePropChangeHandler);
            var loc9=ObjCreate.createBitmap(ImagesConst.Tongqian, 200, 155, this);
            this._nowRootLevelTxt = this.createTextfield(15, 10, 270, 20, "", this._yellowTf);
            this._nextAddTxt = this.createTextfield(15, 35, 300, 20, "", this._yellowBTf);
            this._successRateTxt = this.createTextfield(150, 70, 80, 30, "", this._greenBTf);
            this._costTxt = this.createTextfield(155, 155, 50, 20, "", GlobalStyle.textFormat1);
            this._protectPropNum = this.getPropNum(ECategory._ECategoryProp, EProp._EPropRootProtect);
            this._propNumTxt = this.createTextfield(155, 177, 50, 20, this._protectPropNum + Language.getString(30127), GlobalStyle.textFormat1);
            this._autoBuyCb = ObjCreate.createCheckBox(Language.getString(30128), 80, 130, 200, 20, this);
            this._shopItemInfo = ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropRootProp, 1, -1, true, ShopRule.ShopMallProp);
            if (this._shopItemInfo != null) 
            {
                loc10 = new ItemData(this._shopItemInfo.itemInfo.item.code);
                this._item.itemData = loc10;
                this._rootPropNum = this.getPropNum(ECategory._ECategoryProp, EProp._EPropRootProp);
                this._item.setAmountText(this._rootPropNum);
            }
            this._roleCache = Cache.instance.role;
            return;
        }

        /* internal  */addPerfectPropBtn(arg1: int): void
        {
            if (arg1 < 70) 
            {
                if (this._perfectPropBtn && this._perfectPropBtn.parent) 
                {
                    this._perfectPropBtn.visible = false;
                    removeChild(this._perfectPropBtn);
                }
                if (this._perfectPropItem && this._perfectPropItem.parent) 
                {
                    this._perfectPropItem.visible = false;
                    removeChild(this._perfectPropItem);
                }
            }
            else 
            {
                if (this._perfectPropBtn == null) 
                {
                    this._perfectPropBtn = new EquipPropButton(Language.getString(30336));
                    this._perfectPropBtn.x = 200;
                    this._perfectPropBtn.y = 95;
                    this._perfectPropBtn.useHandCursor = true;
                    this._perfectPropBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onPerfectPropBtnClickHandler);
                }
                this._perfectPropBtn.visible = true;
                addChild(this._perfectPropBtn);
                if (this._perfectPropItem == null) 
                {
                    this._perfectPropItem = new PropItem();
                    this._perfectPropItem.x = 200;
                    this._perfectPropItem.y = 95;
                    this._perfectPropItem.setSize(33, 33);
                    this._perfectPropItem.addEventListener(flash.events.MouseEvent.CLICK, this.onPerfectPropItemClickHandler);
                }
                this._perfectPropItem.visible = false;
                addChild(this._perfectPropItem);
            }
            this._isPerfectProp = false;
            return;
        }

        /* internal  */onPerfectPropBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc1=Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootPerfectUpgradeRune, this._rootNLevel);
            if (loc1 > 0) 
            {
                loc2 = ItemConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropRootPerfectUpgradeRune, -1, this._rootNLevel);
                if (loc2 && loc2.item) 
                {
                    this._perfectPropItem.itemData = new ItemData(loc2.item.code);
                }
                this._perfectPropBtn.visible = false;
                this._perfectPropItem.visible = true;
                this._successRateTxt.text = "100%";
                this._isPerfectProp = true;
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(30337, this._rootNLevel));
            }
            return;
        }

        /* internal  */createTextfield(arg1: int, arg2: int, arg3: int, arg4: int, arg5: string, arg6: Object /* flash.text.TextFormat */): Object /* flash.text.TextField */
        {
            var loc1;
            (loc1 = new Object /* flash.text.TextField */()).selectable = false;
            loc1.mouseEnabled = false;
            loc1.multiline = true;
            loc1.wordWrap = true;
            loc1.x = arg1;
            loc1.y = arg2;
            loc1.width = arg3;
            loc1.height = arg4;
            loc1.defaultTextFormat = arg6;
            loc1.text = arg5;
            addChild(loc1);
            return loc1;
        }

        /* internal  */getPropNum(arg1: int, arg2: int): int
        {
            var loc1=0;
            var loc3=null;
            var loc2=Cache.instance.pack.backPackCache.getAllItems();
            var loc4=0;
            while (loc4 < loc2.length) 
            {
                if (loc2[loc4] != null) 
                {
                    if ((loc3 = loc2[loc4]) && !(loc3 == ItemData.nullItemData) && loc3.itemInfo) 
                    {
                        if (loc3.itemInfo.item.category == arg1 && loc3.itemInfo.item.type == arg2) 
                        {
                            loc1 = loc1 + loc3.itemAmount;
                        }
                    }
                }
                ++loc4;
            }
            return loc1;
        }

        /* internal  */onBuyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=parseInt(this._numInput.text);
            if (loc1 > 99) 
            {
                loc1 = 99;
                this._numInput.text = "99";
            }
            if (loc1 <= 0) 
            {
                return;
            }
            var loc2=loc1 * this._shopItemInfo.tshopSell.price;
            var loc3=Language.getStringByParam(30129, loc2, loc1);
            Alert.show(loc3, null, Alert.OK | Alert.CANCEL, null, this.onClickHandler);
            return;
        }

        /* internal  */onClickHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": _shopItemInfo, "amount": parseInt(this._numInput.text)}));
                Alert.resetToDefault();
            }
            this._numInput.text = "1";
            return;
        }

        /* internal  */onUpgradeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc3=null;
            var loc4=0;
            var loc1=AcupointData.getAcupointCurLevel(this._step, AcupointData.acupointInfo);
            var loc2=AcupointData.getRootLevel(this._step);
            if (loc1 > 0) 
            {
                if (this._rootPropNum <= 0) 
                {
                    if (this._autoBuyCb.selected && SystemSetter.currentSetter.isRoot) 
                    {
                        if ((!this._usePropCb.selected || this._protectPropNum <= 0) && !this._isPerfectProp && loc2 >= 60 && loc2 <= 90) 
                        {
                            loc3 = Language.getString(30265);
                            Alert.show(loc3, null, Alert.OK | Alert.CANCEL, null, this.onNoPerfectPropClickHandler);
                        }
                        else 
                        {
                            this._isAutoBuy = true;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": _shopItemInfo, "amount": 1}));
                        }
                    }
                    else 
                    {
                        QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropRootProp, 1, -1, true, ShopRule.ShopMallProp);
                        QuickBuyWindow.instance.updateDefaultAmount(1);
                        QuickBuyWindow.instance.show();
                    }
                    return;
                }
                if (!SystemSetter.currentSetter.isRoot && !this._isPerfectProp) 
                {
                    loc3 = Language.getString(30130);
                    Alert.show(loc3, null, Alert.OK | Alert.CANCEL, null, this.onUpgradeClickHandler);
                }
                else if (this._roleCache.money.coin + this._roleCache.money.coinBind < this._tAcupointConfig.cost) 
                {
                    loc4 = this._tAcupointConfig.cost - (this._roleCache.money.coin + this._roleCache.money.coinBind);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc4));
                }
                else if (this._usePropCb.selected == true && this._protectPropNum <= 0 && !this._isPerfectProp) 
                {
                    loc3 = "<p align=\'center\'><font color=\'#ffffff\'>" + Language.getString(30131) + "</font></p>";
                    Alert.show(loc3, null, Alert.OK, null, this.onNoProtectClickHandler);
                }
                else if ((!this._usePropCb.selected || this._protectPropNum <= 0) && loc2 >= 60 && loc2 <= 90 && !this._isPerfectProp) 
                {
                    loc3 = Language.getString(30265);
                    Alert.show(loc3, null, Alert.OK | Alert.CANCEL, null, this.onProtectClickHandler);
                }
                else 
                {
                    this.upgradeRoot();
                }
            }
            else 
            {
                Alert.show(Language.getString(30132), null, Alert.OK, null, this.onTipClickHandler);
            }
            return;
        }

        /* internal  */upgradeRoot(): void
        {
            var loc1=0;
            if (this._usePropCb.selected) 
            {
                loc1 = 1;
            }
            var loc2=false;
            if (this._perfectPropItem && this._perfectPropItem.visible) 
            {
                loc2 = true;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointUpgradeRoot, {"step": _step, "useProtectNum": loc1, "perfectUpgrade": loc2}));
            return;
        }

        /* internal  */onNoPerfectPropClickHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this._isAutoBuy = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": _shopItemInfo, "amount": 1}));
            }
            Alert.resetToDefault();
            return;
        }

        /* internal  */onUpgradeClickHandler(arg1: int): void
        {
            var loc1=0;
            var loc2=false;
            if (arg1 == Alert.OK) 
            {
                loc1 = 0;
                if (this._usePropCb.selected) 
                {
                    loc1 = 1;
                }
                loc2 = false;
                if (this._perfectPropItem && this._perfectPropItem.visible) 
                {
                    loc2 = true;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointUpgradeRoot, {"step": _step, "useProtectNum": loc1, "perfectUpgrade": loc2}));
                SystemSetter.localSetter.isRoot = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
            }
            Alert.resetToDefault();
            return;
        }

        /* internal  */onNoProtectClickHandler(arg1: int): void
        {
            Alert.resetToDefault();
            return;
        }

        /* internal  */onProtectClickHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.upgradeRoot();
            }
            Alert.resetToDefault();
            return;
        }

        /* internal  */onUsePropChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._protectPropNum > 0) 
            {
                if (this._usePropCb.selected) 
                {
                    this._useProtect = true;
                }
                else 
                {
                    this._useProtect = false;
                }
            }
            else 
            {
                this._usePropCb.selected = false;
                this._useProtect = false;
                MsgManager.showRollTipsMsg(Language.getString(30133));
            }
            return;
        }

        public updateAttInfo(arg1: int): void
        {
            var loc9=0;
            var loc10=0;
            var loc1=AcupointData.getAttributesByStep(arg1, 1, AcupointData.acupointInfo.attributes);
            var loc2=AcupointData.getAttributesByStep(arg1, 3, AcupointData.acupointInfo.attributes);
            var loc3=AcupointData.getAttributeStr(loc1, "", Cache.instance.role.careerBasic, Cache.instance.role.careerEnabled, true, false);
            this._rootLevel = AcupointData.getRootLevel(arg1);
            this._rootNLevel = this._rootLevel + 10;
            var loc4=Language.getString(30135) + "  " + this._rootLevel + " → " + this._rootNLevel + Language.getString(30125);
            var loc5=(loc2[0] as SAttributeUpdate).value;
            this._nowRootLevelTxt.text = loc4 + "\t\t" + loc3 + "→" + loc5;
            var loc6;
            var loc7=(loc6 = AcupointData.acupointInfo.rootEffects)[arg1];
            var loc8;
            if (loc8 = loc1[0] as SAttributeUpdate) 
            {
                loc10 = (loc9 = loc8.value) / (loc7 + 100) * 100;
                this._nextAddTxt.text = Language.getString(30257) + loc10 + "\t" + Language.getString(30258) + (loc9 - loc10);
            }
            return;
        }

        public updateConfigInfo(arg1: int): void
        {
            this._step = arg1;
            var loc1=AcupointData.getAcupointCurLevel(arg1, AcupointData.acupointInfo);
            var loc2=AcupointData.getRootLevel(arg1);
            var loc3=loc2 + 10;
            if (loc2 == 100) 
            {
                loc3 = 101;
            }
            this._tAcupointConfig = AcupointConfig.instance.getRootInfoByTarget(loc2);
            this._successRateTxt.text = this._tAcupointConfig.rate + "%";
            this._costTxt.text = this._tAcupointConfig.cost.toString();
            if (this._tAcupointConfig.cost > this._roleCache.money.coin + this._roleCache.money.coinBind) 
            {
                this._costTxt.htmlText = "<font color=\'#ff0000\'>" + this._tAcupointConfig.cost + "</font>";
            }
            else 
            {
                this._costTxt.setTextFormat(GlobalStyle.textFormat1);
            }
            this.updateProtectProp();
            this.updateAttInfo(arg1);
            this.addPerfectPropBtn(loc2);
            return;
        }

        public updateProtectProp(): void
        {
            var loc1=0;
            var loc2=false;
            this._protectPropNum = this.getPropNum(ECategory._ECategoryProp, EProp._EPropRootProtect);
            this._propNumTxt.text = this._protectPropNum + Language.getString(30127);
            this._propNumTxt.setTextFormat(GlobalStyle.textFormat1);
            if (this._protectPropNum <= 0 && this._usePropCb.selected) 
            {
                this._useProtect = false;
                this._useProtect = false;
            }
            this._rootPropNum = this.getPropNum(ECategory._ECategoryProp, EProp._EPropRootProp);
            this._item.setAmountText(this._rootPropNum);
            if (this._isAutoBuy && this._rootPropNum > 0) 
            {
                this._isAutoBuy = false;
                loc1 = 0;
                if (this._usePropCb.selected) 
                {
                    loc1 = 1;
                }
                loc2 = false;
                if (this._perfectPropItem && this._perfectPropItem.visible) 
                {
                    loc2 = true;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointUpgradeRoot, {"step": _step, "useProtectNum": loc1, "perfectUpgrade": loc2}));
            }
            return;
        }

        public resetProtect(): void
        {
            if (this._usePropCb.selected) 
            {
                this._usePropCb.selected = false;
                this._useProtect = false;
            }
            return;
        }

        /* internal */ /* var */_roleCache: RoleCache;

        /* internal */ /* var */_autoBuyCb: GCheckBox;

        /* internal */ /* var */_perfectPropBtn: EquipPropButton;

        /* internal */ /* var */_perfectPropItem: PropItem;

        /* internal */ /* var */_item: BaseItem;

        /* internal */ /* var */_rootLevel: int;

        /* internal */ /* var */_isPerfectProp: Boolean=false;

        /* internal */ /* var */_yellowTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_yellowSTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_yellowBTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_rootNLevel: int;

        /* internal */ /* var */_greenSTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_greenBTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_numInput: GTextInput;

        /* internal */ /* var */_buyBtn: GButton;

        /* internal */ /* var */_isAutoBuy: Boolean=false;

        /* internal */ /* var */_upgradeBtn: GButton;

        /* internal */ /* var */_usePropCb: GCheckBox;

        /* internal */ /* var */_nowRootLevelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_nextAddTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_successRateTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_costTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_protectPropNum: int;

        /* internal */ /* var */_propNumTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_shopItemInfo: ShopItemInfo;

        /* internal */ /* var */_tAcupointConfig: TAcupointConfig;

        /* internal */ /* var */_useProtect: Boolean=false;

        /* internal */ /* var */_step: int;

        /* internal */ /* var */_rootPropNum: int;
    }
